package com.letoken.platform.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.letoken.platform.app.service.QRCodeService;
import com.letoken.platform.pub.constant.LetokenUserConstant;
import com.letoken.platform.pub.util.QRCodeUtil;
//import com.letoken.platform.repo.app.manager.AppUserAddressManager;
import com.letoken.platform.repo.app.manager.AppUserManager;
import com.letoken.platform.repo.app.po.AppUser;
import com.letoken.platform.repo.app.redis.AppUserQRCodeRedis;
import com.letoken.platform.repo.app.req.QRCode.QRCodeReq;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * @author wyl
 * @date 2023/5/2 17:07
 * @Version 1.0
 */
@Service
@Log4j2
public class QRCodeServiceImpl implements QRCodeService {

    @Autowired
    private AppUserManager appUserManager;

//    @Autowired
//    private AppUserAddressManager appUserAddressManager;

    @Override
    public String getQRCode(QRCodeReq req) {
        AppUser appUser = appUserManager.queryMe();
        Integer userId = appUser.getId();
        String value = AppUserQRCodeRedis.me().getValue(userId);
        if (StrUtil.isNotBlank(value)) {
            return value;
        }
        String content = UriComponentsBuilder.fromUriString(LetokenUserConstant.OFFICIAL_WEBSITE)
                //id
                .queryParam("id", userId)
                //名称
                .queryParam("userName", appUser.getUserName())
                // 邀请码
                .queryParam("invitationCode", appUser.getInvitationCode())
                // 头像
                .queryParam("headPortrait", appUser.getHeadPortrait())
                // 用户钱包地址 app_user_address 可能不需要传 因为转账操作可以根据id转 TODO 暂不需要
//                .queryParam("userAddress", appUserAddressManager.queryWalletByUserId(userId))
                .build().encode().toString();
        String qrCode = QRCodeUtil.getBase64QRCode(content, req.getWidth(), req.getHeight());
        AppUserQRCodeRedis.me().putValue(userId, qrCode);
        return qrCode;
    }
}
